#!/usr/bin/env python
# -*- coding: UTF-8 -*-
'''
@File    ：sms_id_base_v1.py
@IDE     ：PyCharm 
@Author  ：lmy
@Date    ：2024/8/14 15:17 
'''
import os
from feature_set.sms.utils.data_process import *
from feature_set.sms.utils.data_utils import *
from feature_set.sms.utils.sms_un_base_v1 import SmsUnBaseV1
from feature_set.base_feature import BaseFeature, RequstData
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.metrics.pairwise import cosine_similarity
import math
import warnings

warnings.filterwarnings("ignore")


class SmsIdBaseV1(BaseFeature):
    prefix = 'sms_id_base_v1'

    def __init__(self):
        super().__init__()

        self.load_conf()
        self.country_code = 'ID'
        self.language = 'indonesian'
        self.function_map = {
            'cnt_feature': self.cnt_feature,
            'se_feature': self.se_feature,
            'day_feature': self.day_feature,
            'dod_feature': self.dod_feature,
            'top_feature': self.top_feature,
            'similar_paris_feature': self.similar_paris_feature

        }

    def load_conf(self):
        """
        加载配置文件，每次初始化对象的时候进行调用
        """
        conf_path = self.sms_conf_dir / 'id'

        # 运营商
        file_path = os.path.join(conf_path, 'sms_id_base_v1', 'id_carrier_v1.txt')
        carriers = []
        with open(file_path, 'r', encoding='utf-8') as file:
            for line in file:
                carriers.append(line.strip())

        self.conf = {
            "stopwords": STOPWORD_ID,
            "carrier": carriers
        }

    def load_request(self, request_data: RequstData):
        """
        在该方法中整理数据，对于请求中的数据进行预处理以后，挂载到generator对象上，方便后续的计算函数直接调用
        """
        user_sms = data_processing(request_data.data_sources, request_data.apply_time, 'sms_data', self.country_code,
                                   self.language)
        self.data = user_sms
        self.data['is_carrier'] = self.data['sender'].apply(lambda x: is_carrier(x, self.conf['carrier']))

    def cnt_feature(self):
        sms_feature_res = SmsUnBaseV1.extract_cnt_feature(self.data)
        return sms_feature_res

    def similar_paris_feature(self):
        sms_feature_res = SmsUnBaseV1.extract_similar_paris_feature(self.data, self.conf)
        return sms_feature_res

    def top_feature(self):
        sms_feature_res = SmsUnBaseV1.extract_top_feature(self.data)
        return sms_feature_res

    def day_feature(self):
        sms_feature_res = SmsUnBaseV1.extract_day_feature(self.data)
        return sms_feature_res

    def se_feature(self):
        sms_feature_res = SmsUnBaseV1.extract_se_feature(self.data)
        return sms_feature_res

    def dod_feature(self):
        sms_feature_res = SmsUnBaseV1.extract_dod_feature(self.data)
        return sms_feature_res
